Wednesday, June 16, 2004

Exercise[35]

Insertion Sort (w/ Shuffle)

Pre-work

  1. In Barron's AP Computer Science A, read about Insertion Sort on p. 325.
  2. Optional: Read about insertion sort in another text book that will be made available to you in class.
  3. Explore https://www.tutorialspoint.com/compile_java_online.php.
  4. Study this Java implementation of insertion sort.
  5. Read the multiple choice questions on sorting and searching on pp. 331-345 of Barron's AP Computer Science A. Attempt to answer the questions that are related to insertion sort and then check your answer(s) using the answers provided on pp. 346-350.

Questions

  1. [1 point] Which questions in Barron's AP Computer Science A (pp. 331-345) involve or are related to insertion sort? List the question numbers only.
  2. [2 points] What determines whether this Java implementation of insertion sort sorts elements in ascending or descending order?

Create Activity

  1. [3 points] Create a multiple choice question about insertion sort. You question should include five choices (a - e) for the answer and only one correct answer. Your question may be inspired by a question from Barron's AP Computer Science A but must be substantially different, i.e. you should feel confident that no one would ever accuse you of plagiarizing the question.
  2. [15 points] Study DoTheShuffle. In the body of the email that you submit to the graders or in a single attached file, submit a single Java program that satisfies the following requirements:
    • [5 points] Place all of the classes need to compile and run DoTheShuffle in a single file and verify that the code compiles and runs. If code that you submit compiles, runs, and produces output, then you will earn the points for this portion of the activity.
    • [5 points] Modify the implementation of DoTheShuffle so that it outputs the original sequence, the sequence sorted in ascending order, the sequence sorted in descending order, and the sequence after shuffling. The output of the modified program should contain one more line of output than the original version of the program.
    • [5 points] Modify the implementation of the maneuver method of LazyStudentShuffler so that it actually shuffles the values of the array argument called seq. Make sure that your program runs as expected for input sequences comprised of zero elements (1 point), one element (1 point), two elements (1 point), and more than two elements (2 points).

Exercise[34]

Binary Search

Pre-work

  1. In Barron's AP Computer Science A, read about Binary Search on p. 329-330.
  2. Optional: Read about binary search in another text book that will be made available to you in class.
  3. Study and execute these Java implementations of binary search.
  4. Read the multiple choice questions on sorting and searching on pp. 331-345 of Barron's AP Computer Science A. Attempt to answer the questions that are related to binary search and then check your answer(s) using the answers provided on pp. 346-350.

Questions

  1. [1 point] Which questions in Barron's AP Computer Science A (pp. 331-345) involve or are related to binary search? List the question numbers only.
  2. [1 points] What is the difference between the Java implementations of binary search that implement Search<T> and the ones that implement MeteredSearch<T>?
  3. [2 points] Compare and contrast RecursiveBinarySearch and RepetitiveBinarySearch.

Create Activity

  1. [3 points] Create a multiple choice question about binary search. You question should include five choices (a - e) for the answer and only one correct answer. Your question may be inspired by a question from Barron's AP Computer Science A but must be substantially different, i.e. you should feel confident that no one would ever accuse you of plagiarizing the question.
  2. [6 points] Create two implementations of RecursiveBinaryMeteredSearch or RepetitiveBinaryMeteredSearch that are different from the ones provided. (Consider replacing the switch statement with if-else statements or calculating the value of middle differently.) Both implementations must compile and must implement MeteredSearch<T>. One implementation should be correct (to the best of your knowledge) and the other implementation should be flawed.
  3. [3 points] Indicate which of the two implementations of binary search that you are submitting is flawed and describe the flaw.

Exercise[33]

Sequential Search

Pre-work

  1. In Barron's AP Computer Science A, read about Sequential Search on p. 329.
  2. Read about sequential search in one or more of the following books, which will be made available to you in class:
    • Volume 3 (first edition) of The Art of Computer Programming (by D. E. K.), pp. 393-402.
    • Volume 3 (second edition) of The Art of Computer Programming (by D. E. K.), pp. 396-405.
    • Programming Classics (by Ian Oliver), pp. 196-198 and p. 200.
  3. Implement sequential search in Java with and without the use of a sentinel value.
  4. Read the multiple choice questions on sorting and searching on pp. 331-345 of Barron's AP Computer Science A. Attempt to answer the questions that are related to sequential search and then check your answer(s) using the answers provided on pp. 346-350.

Questions

  1. [1 point] Which questions in Barron's AP Computer Science A (pp. 331-345) involve or are related to sequential search? List the question numbers only.
  2. [2 points] List two topics related to sequential search that are discussed by Donald E. Knuth (in The Art of Computer Programming) or Ian Oliver (in Programming Classics) that are not discussed in Barron's AP Computer Science A.
  3. [2 points] What is a sentinel value and how can one be used in a sequential search algorithm?

Create Activity

  1. [3 points] Create a multiple choice question about sequential search. You question should include five choices (a - e) for the answer and only one correct answer. Your question may be inspired by a question from Barron's AP Computer Science A but must be substantially different, i.e. you should feel confident that no one would ever accuse you of plagiarizing the question.
  2. [3 points] Create another multiple choice question about sequential search. You question should include five choices (a - e) for the answer and only one correct answer. Your question must be related to a topic not discussed on p. 329 of Barron's AP Computer Science A.
  3. [3 points] Create an answer key for your two questions. For each question, the answer key should explain why the correct answer is correct and why the incorrect answers are incorrect.

Exercise[32]

Pre-work

  1. Read the lecture notes published at the following URL. Pay close attention to the Fibonacci example and the section on memoization.
    http://courses.csail.mit.edu/6.01/spring07/lectures/lecture4.pdf
  2. See also:
  3. Examine the code, read the comments, and follow the links posted at:
  4. Several reports, each listing a series of input values, output values, operation counts and run times for various "Fib functions" will be distributed in class. Study the data. Look for interesting patterns and surprising results.
  5. Become more familiar with the algorithms and their implementations by performing your own experiments using both the Java code (Funky Fibs and Big Fibs) and the JavaScript code (Funky Fibs).

    You can run the Java code as follows:

    1. Paste the code into the online editor at https://www.compilejava.net/. (Be sure to overwrite the code that appears in the editor by default.)
    2. Enter one or more command line arguments in the input field that appears immediately below the text editor and above the buttons.
    3. Press the COMPILE & EXECUTE button.

Assignment

  1. What is the common problem that all of the "Fib functions" (Java and JavaScript versions) are designed to solve?
  2. How many distinct algorithms are used to solve the problem?
  3. In English, describe how each algorithm works.
  4. Compare Funky Fibs (Java) with Big Fibs (Java). What is the main difference between the two implementations?
  5. Compare and contrast each distinct algorithm. In your analysis, answer the following questions.
    1. How easy or difficult do you think it is to understand the algorithm, implement it in code, and verify that it has been implemented correctly?
    2. How does the number of mathematical operations the algorithm performs vary as a function of the size of the inputs as well as the sequencing of successive inputs?
    3. How much space is required to store data used by the algorithm? Is the amount of space needed significant?
  6. Compare and contrast the implementations of the algorithms. In your analysis, answer the following questions.
    1. When does the implementation produce correct and incorrect results?
    2. Discuss the performance (the time it takes to produce an answer) of each implementation. Are the performance differences significant?
  7. What's the connection between each of following names and its corresponding "Fib function"?
    • Amnesic
    • Eidetic
    • Grinder
    • Unitary
    • Lacunar

Exercise[31]

Play Rock-Paper-Scissors

You are player 1. I am player 2.

A Rock-Paper-Scissors Tournament

The code displayed when you press the first four buttons below implements a rock-paper-scissors tournament. When viewing the JavaScript version of the code, a fifth button labeled Try to win by: followed by a value (3 by default) is visible.

Assignment

Question and Coding (100 points + possible bonus points)

  1. Describe the range of values that Math.random may return. (10 points)
  2. Modify Utility.randomInt (under To Do) so that it returns a random integer between the values of min and max inclusive. (20 points)
  3. Modify KingOfTheHill (under To Do) so that an instance of KingOfTheHill:
    • always wins at least one out of three matches in the Rock-Paper-Scissors tournament described above. (30 points)
    • always wins at least two out of three matches in the Rock-Paper-Scissors tournament. (15 points)
    • always wins at least two out of three matches in the fewest number of rounds. (pride)
  4. Submit your answer and your code for Utility.randomInt and KingOfTheHill by end of day, Tuesday, 3 January 2017 (extended due to snow days):
    • Email your answer and code to jspurgeon@vcstudent.org with the subject "Exercise[31]". (10 points)
    • Work submitted after the due date will be penalized 5 points per day late, up to 20 points.

How to Play in Java

@ www.compilejava.net ...

class PlayRPS {
  public static void main(String[] args) {
    RPSMatch match = new RPSMatch(null, null);
    match.setChoiceOfPlayer1(args[0]);
    match.setIndexOfChoiceOfPlayer2(Utility.randomInt(0, 2));
    System.out.println(match.getReplay());
  }
}

How We Play (Above)

function PlayRPS(choice) {
  var match = new RPSMatch(null, null);
  match.setChoiceOfPlayer1(choice);
  match.setIndexOfChoiceOfPlayer2(Utility.randomInt(0, 2));
  var results = match.getReplay();
  console.log(results);
  if (document.getElementById('RPSAlertFlag').selectedIndex === 0) {
    alert(results);
  }
}

Exercise[30]

Finish implementing the Java or JavaScript Util methods shown below (i.e. toArray, copyOfArray, swap, flip, flipCopyOfArray, and flipCopyOfString).

When you are finished, send an email to jspurgeon@vcstudent.org with the subject "Exercise[30]". Include your completed functions in the body of the email message or include a reference to the code in the message.

Grading Rubric

  • Each correctly completed method is worth 10 points.
  • Following the instructions above is worth 10 points.
  • Work turned in late will be penalized 10 points.

Due Date

End of day Mon, 5 Dec 2016

Note

java FlippantFunctions "abc" should output:
abc
cba
abc
cba

FlippantFunctions.main(["abc"]) should output:

abc
cba
["a", "b", "c"]
["c", "b", "a"]